﻿//using Commons;
//using Entity.Project;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;

//namespace Bussiness.Mangement.Project
//{
//    public class PlayingCardBussiness
//    {
//        public static PlayingCardBussiness Init { get => new PlayingCardBussiness(); }

//        #region 德州扑克

//        /// <summary>
//        /// 德州扑克牌组
//        /// </summary>
//        private static List<PlayingCard> _texasHoldEMPoker;
//        /// <summary>
//        /// 德州扑克牌组
//        /// </summary>
//        public static List<PlayingCard> TexasHoldEMPoker
//        {
//            get
//            {
//                if (_texasHoldEMPoker == null || _texasHoldEMPoker.Count<52)
//                {
//                    _texasHoldEMPoker = Init.GetTexasHoldEMPoker();
//                }
//                return _texasHoldEMPoker;
//            }
//        }

//        /// <summary>
//        /// 获取德州扑克牌组
//        /// </summary>
//        private List<PlayingCard> GetTexasHoldEMPoker()
//        {
//            List<PlayingCard> list = new List<PlayingCard>();
//            var hsList = GetDPFlowerColors();//花色
//            var pmList = GetDPReadingTheBoards();//牌面
//            foreach (var hs in hsList)
//            {
//                foreach (var pm in pmList)
//                {
//                    list.Add(new PlayingCard( hs,pm) );
//                }
//            }
//            return list;
//        } 

//        /// <summary>
//        /// 德州扑克花色
//        /// </summary>
//        /// <returns></returns>
//        public List<FlowerColor> GetDPFlowerColors()
//        {
//            return new List<FlowerColor>() {
//                new FlowerColor(){Name= FlowerColorType.FP,Value=0},//方片♦
//                new FlowerColor(){Name=FlowerColorType.MH,Value=1},//梅花♣
//                new FlowerColor(){Name=FlowerColorType.RT,Value=2},//红桃♡
//                new FlowerColor(){Name=FlowerColorType.BT,Value=3}//黑桃♤
//            };
//        }
        
//        /// <summary>
//        /// 德州扑克牌面
//        /// </summary>
//        /// <returns></returns>
//        public List<ReadingTheBoard> GetDPReadingTheBoards()
//        {
//            return new List<ReadingTheBoard>()
//            {
//                new ReadingTheBoard(){Name="2",Value=0},
//                new ReadingTheBoard(){Name="3",Value=1},
//                new ReadingTheBoard(){Name="4",Value=2},
//                new ReadingTheBoard(){Name="5",Value=3},
//                new ReadingTheBoard(){Name="6",Value=4},
//                new ReadingTheBoard(){Name="7",Value=5},
//                new ReadingTheBoard(){Name="8",Value=6},
//                new ReadingTheBoard(){Name="9",Value=7},
//                new ReadingTheBoard(){Name="10",Value=8},
//                new ReadingTheBoard(){Name="J",Value=9},
//                new ReadingTheBoard(){Name="Q",Value=10},
//                new ReadingTheBoard(){Name="K",Value=11},
//                new ReadingTheBoard(){Name="A",Value=12}
//            };
//        }

//        /// <summary>
//        /// 根据玩家人数自动生成对局牌组
//        /// 随机生成
//        /// </summary>
//        /// <param name="playerNum">玩家人数</param>
//        /// <returns></returns>
//        public GamePlayingCard AutoCrateDZPKGame(int playerNum)
//        {
//            var result = new GamePlayingCard() { CommunityCards = new List<PlayingCard>(), PlayersCards = new List<List<PlayingCard>>(),Key=Guid.NewGuid().ToString() };
//            var nowCards = TexasHoldEMPoker;
//            if (nowCards.Count<(playerNum*2+5))
//            {
//                return null;
//            }
//            //生成5张公共牌
//            for (int i = 0; i < 5; i++)
//            {
//                //随机获取到一张牌
//                var num = new Random(Guid.NewGuid().GetHashCode()).Next(0, nowCards.Count);
//                var autoCard = nowCards[num];
//                //添加到公共牌中
//                result.CommunityCards.Add(autoCard);
//                //在当前牌中删除掉已被选过的牌
//                nowCards.RemoveAt(num);
//            }
//            //生成所有玩家手牌
//            for (int i = 0; i < playerNum; i++)
//            {
//                List<PlayingCard> playerCards = new List<PlayingCard>();//单个玩家手牌集合
//                //每位玩家的两张手牌
//                for (int a = 0; a < 2; a++)
//                {
//                    //随机获取到一张牌
//                    var num = new Random(Guid.NewGuid().GetHashCode()).Next(0, nowCards.Count);
//                    var autoCard = nowCards[num];
//                    //添加到当前玩家手牌中
//                    playerCards.Add(autoCard);
//                    //在当前牌中删除掉已被选过的牌
//                    nowCards.RemoveAt(num);
//                }
//                result.PlayersCards.Add(playerCards);
//            }
//            return result;
//        }

//        /// <summary>
//        /// 根据手牌和公共牌，获得最佳的五张牌牌组
//        /// </summary>
//        /// <param name="communityCards">公共牌</param>
//        /// <param name="playerCards">手牌</param>
//        /// <returns></returns>
//        public Wands GetOptimalWands(List<PlayingCard> communityCards,List<PlayingCard> playerCards)
//        {
//            if (communityCards==null || communityCards.Count<=0 || playerCards==null || playerCards.Count<=0)
//            {
//                return null;
//            }
//            List<PlayingCard> cards = new List<PlayingCard>();
//            cards.AddRange(communityCards);
//            cards.AddRange(playerCards);
//            //cards=cards.OrderByDescending(x => x.Value).ToList();//将牌从高到地排列
//            FlowerColor flowerColor;//花色
//            ReadingTheBoard readingTheBoard;//高牌面
//            bool isTH = false; //是否同花
//            bool isSZ = false;//是否顺子
//            //依次按照 皇家同花顺、同花顺、四条、葫芦、同花、顺子、三条、两对、一对、高牌 的顺序找
//            isTH = IsExistTH(cards, out flowerColor,out readingTheBoard);
//            if (isTH)
//            {
//                //是同花再判断是不是同花顺
//                var thCard = cards.FindAll(x => x.FlowerColor == flowerColor);//同花的牌
//                isSZ = isExistSZ(thCard, out flowerColor, out readingTheBoard); 
//                if (isSZ)
//                {
//                    var resultCards = thCard.OrderByDescending(x => x.ReadingTheBoard.Value).Take(5).ToList();
//                    //判断高牌是不是黑桃A
//                    if (flowerColor.Name== FlowerColorType.BT && readingTheBoard.Name=="A")
//                    {
//                        //1.皇家同花顺----------
//                        Wands result = new Wands(WandsLevel.RoyalFlush, flowerColor, readingTheBoard, resultCards);
//                        return result;
//                    }
//                    else
//                    {
//                        //2.同花顺----------
//                        Wands result = new Wands(WandsLevel.StralghtFlush, flowerColor, readingTheBoard, resultCards);
//                        return result;
//                    }
//                }
//            }
//            List<PlayingCard> equalList;
//            ReadingTheBoard eqReading;
//            int equalNum = GetEqualNum(cards,out eqReading, out equalList);//相同牌面的数量
//            List<PlayingCard> otherList = cards.Except(equalList).ToList();//除相同牌余下的拍
//            if (equalNum==4)
//            {
//                //3.四条----------
//                equalList.Add(otherList.OrderByDescending(x => x.ReadingTheBoard.Value).First());
//                var resultCards = equalList;
//                Wands result = new Wands(WandsLevel.FourOfAKind, null, eqReading, resultCards);
//                return result;
//            }
//            ReadingTheBoard otherrt;List<PlayingCard> otherpcs;
//            int otherNum = GetEqualNum(otherList, out otherrt, out otherpcs);
//            if (equalNum==3 && otherNum==2)
//            {
//                //4.葫芦----------
//                var resultCards = otherList.Union(otherpcs).ToList();
//                Wands result = new Wands(WandsLevel.FullHouse, null, eqReading, resultCards);
//                return result;

//            }
//            if (isTH)
//            {
//                //5.同花----------
//                var thCard = cards.FindAll(x => x.FlowerColor == flowerColor);//同花的牌
//                var resultCards = thCard.OrderByDescending(x => x.ReadingTheBoard.Value).Take(5).ToList();
//                Wands result = new Wands(WandsLevel.Flush, flowerColor, readingTheBoard, resultCards);
//                return result;

//            }
//            isSZ = isExistSZ(cards, out flowerColor, out readingTheBoard);
//            if (isSZ)
//            {
//                //6.顺子----------
//                //找到顺子最大的牌
//                var sxlist = cards.OrderByDescending(x => x.ReadingTheBoard.Value).ToList();
//                var index = sxlist.FindIndex(x => x.FlowerColor == flowerColor && x.ReadingTheBoard == readingTheBoard);
//                sxlist = sxlist.Except(sxlist.Take(index + 1)).ToList();
//                if (sxlist.Count < 5)
//                    throw new Exception("牌组无顺子，IsExistSZ函数出错。");
//                var resultCards = sxlist.Take(5).ToList();
//                Wands result = new Wands(WandsLevel.Straight, flowerColor, readingTheBoard, resultCards);
//                return result;

//            }
//            if (equalNum==3)
//            {
//                //7.三条----------
//                var resultCards = equalList;
//                resultCards.AddRange(otherList.OrderByDescending(x => x.ReadingTheBoard.Value).Take(2));
//                Wands result = new Wands(WandsLevel.ThreeOfAKind, null, equalList.FirstOrDefault().ReadingTheBoard, resultCards);
//                return result;
//            }
//            otherNum = GetEqualNum(otherList, out otherrt, out otherpcs);
//            if (equalNum == 2 && otherNum == 2)
//            {
//                //8.两对----------
//                var resultCards = equalList.Union(otherpcs).ToList();
//                resultCards.Add(cards.Except(resultCards).OrderByDescending(x => x.ReadingTheBoard.Value).First());
//                Wands result = new Wands(WandsLevel.TwoPalr, null, eqReading, resultCards);
//                return result;

//            }
//            if (equalNum==2)
//            {
//                //9.一对----------
//                var resultCards = equalList;
//                resultCards.AddRange(cards.Except(resultCards).OrderByDescending(x => x.ReadingTheBoard.Value).Take(3));
//                Wands result = new Wands(WandsLevel.OnePalr, null, eqReading, resultCards);
//                return result;

//            }
//            if (equalNum==1)
//            {
//                //10.高牌----------
//                var resultCards = cards.OrderByDescending(x => x.ReadingTheBoard.Value).Take(5).ToList();
//                Wands result = new Wands(WandsLevel.HighCard, resultCards[0].FlowerColor, resultCards[0].ReadingTheBoard, resultCards);
//                return result;
//            }

//            return null;
//        }


//        /// <summary>
//        /// 选出最佳牌组
//        /// </summary>
//        /// <param name="list"></param>
//        /// <returns></returns>
//        public List<GameRecord> GetWinWands(List<GameRecord> list)
//        {
//            var winList = new List<GameRecord>();
//            if (list!=null && list.Count>=2)
//            {
//                //采用冒泡排序 将最大的放在最前面
//                for (int a = 0; a < list.Count-1; a++)
//                {
//                    for (int b = a+1; b < list.Count; b++)
//                    {
//                        GameRecord temp ,ga=list[a],gb=list[b];
//                        //等级比下一个小 换位
//                        if (ga.Wands.LevelNum<gb.Wands.LevelNum)
//                        {
//                            temp = ga;
//                            list[a] = gb;
//                            list[b] = temp;
//                        }
//                        //等级相同比较牌面和花色
//                        else if (ga.Wands.LevelNum == gb.Wands.LevelNum)
//                        {
//                            //比较最大牌面 比下一位小 换位
//                            if (ga.Wands.ReadingTheBoard!=null && gb.Wands.ReadingTheBoard!=null && ga.Wands.ReadingTheBoard.Value<gb.Wands.ReadingTheBoard.Value)
//                            {
//                                temp = ga;
//                                list[a] = gb;
//                                list[b] = temp;
//                            }
//                            //牌面相同比较花色 比下一位小 换位
//                            else if (ga.Wands.FlowerColor != null && gb.Wands.FlowerColor != null && ga.Wands.FlowerColor.Value < gb.Wands.FlowerColor.Value)
//                            {
//                                temp = ga;
//                                list[a] = gb;
//                                list[b] = temp;
//                            }
//                            else if(ga.Wands.Cards.Sum(x=>x.Value)<gb.Wands.Cards.Sum(x=>x.Value))
//                            {
//                                temp = ga;
//                                list[a] = gb;
//                                list[b] = temp;
//                            }
//                        }
//                    }
//                }

//                #region 复杂逻辑 暂时不用
//                ////如果胜利的玩家有几轮没上筹码 则相同等级的牌玩家也获胜
//                ////这里的筹码都是负数 所以比小
//                //long min = list.Min(x => x.Jetton);
//                //if (list.First().Jetton > min)
//                //{
//                //    //同等级同胜
//                //}
//                //else
//                //{
//                //    //只有一个人获胜

//                //} 
//                #endregion
//                //添加获胜人
//                winList.Add(list.First());
//            }
//            else
//            {
//                //只有一条以下的数据
//                winList = list;
//            }
//            return winList;
//        }


//        /// <summary>
//        /// 获取相同的牌的数量
//        /// </summary>
//        /// <param name="cards"></param>
//        /// <param name="flowerColor"></param>
//        /// <param name="readingTheBoard"></param>
//        /// <returns></returns>
//        private int GetEqualNum(List<PlayingCard> cards,  out ReadingTheBoard readingTheBoard, out List<PlayingCard> equalList)
//        {
//            readingTheBoard = null;
//            int num = 0;
//            equalList = null;
//            foreach (var card in cards)
//            {
//                var findList = cards.FindAll(x => x.ReadingTheBoard.Name == card.ReadingTheBoard.Name);
//                if (findList!=null )
//                {
//                    if (findList.Count > num)
//                    {
//                        num = findList.Count;
//                        readingTheBoard = card.ReadingTheBoard;
//                        equalList = findList;
//                    }
//                    else if(findList.Count>0 && findList.Count==num && readingTheBoard!=null)
//                    {
//                        if (card.ReadingTheBoard.Value>readingTheBoard.Value)
//                        {
//                            readingTheBoard = card.ReadingTheBoard;
//                            equalList = findList;
//                        }
//                    }
//                }
//            }
//            return num;
//        }
//        /// <summary>
//        /// 是否存在顺子
//        /// </summary>
//        /// <param name="cards"></param>
//        /// <param name="readingTheBoard"></param>
//        /// <returns></returns>
//        private bool isExistSZ(List<PlayingCard> cards, out FlowerColor flowerColor, out ReadingTheBoard readingTheBoard)
//        {
//            readingTheBoard = null;
//            flowerColor = null;
//            cards = cards.OrderByDescending(x => x.ReadingTheBoard.Value).ToList();
//            for (int i = 0; i < cards.Count-4; i++)
//            {
//                int num = 0;//连续相邻的数量
//                FindNextCard(cards, cards[i], ref num);
//                if (num >= 5)
//                {
//                    readingTheBoard = cards[i].ReadingTheBoard;
//                    flowerColor = cards[i].FlowerColor;
//                    return true;
//                }
//            }
//            return false;
//        }
//        /// <summary>
//        /// 递归找到牌面小一点的牌
//        /// </summary>
//        /// <param name="cards">牌组</param>
//        /// <param name="cards">牌面大的牌</param>
//        /// <param name="num">连续相邻的数量</param>
//        private void FindNextCard(List<PlayingCard> cards,PlayingCard card,ref int num)
//        {
//            var result = cards.Find(x=>x.ReadingTheBoard.Value+1==card.ReadingTheBoard.Value);
//            if (result!=null)
//            {
//                num++;
//                FindNextCard(cards, result,ref num);
//            }
//        }
//        /// <summary>
//        /// 是否存在同花
//        /// </summary>
//        /// <param name="cards"></param>
//        /// <param name="flowerColorType"></param>
//        /// <returns></returns>
//        private bool IsExistTH(List<PlayingCard> cards,out FlowerColor flowerColor,out ReadingTheBoard readingTheBoard)
//        {
//            flowerColor =null; readingTheBoard = null;
//            foreach (var item in new List<FlowerColorType>() { FlowerColorType.BT, FlowerColorType.RT, FlowerColorType.MH, FlowerColorType.FP })
//            {
//                var list= cards.FindAll(x => x.FlowerColor.Name == item);
//                if (list!=null && list.Count>=5)
//                {
//                    flowerColor = list.First().FlowerColor;
//                    readingTheBoard = list.OrderByDescending(x => x.ReadingTheBoard.Value).First().ReadingTheBoard;
//                    return true;
//                }
//            }
//            return false;
//        }

//        #endregion

//    }


//}
